package com.eorchis.module.trainingclass.service.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Title: PropertyUtil<br>
 * Description: ���Թ�����<br>
 * Company: ORCHIS<br>
 * Copyright @ 2010 ORCHIS .All rights reserved.<br>
 * 
 * @author ZhangKW
 * @createDate 2010-4-1
 * @version $Revision: 1.2 $
 */
public class PropertyNewUtil {

	/**
	 * ��֤��������Ƿ�Ϊ��
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static boolean PropertyEmpty(Object obj) {
		boolean result = false;
		if (obj == null) {
			return true;
		}
		if (obj instanceof String) {
			if ("".equals(((String) obj).trim())) {
				result = true;
			}
		}
		if (obj instanceof Integer) {
			if (0 == ((Integer) obj).intValue()) {
				result = true;
			}
		}
		if (obj instanceof String[]) {
			if (0 == ((String[]) obj).length) {
				result = true;
			}
		}
		if (obj instanceof Integer[]) {
			if (0 == ((Integer[]) obj).length) {
				result = true;
			}
		}
		if (obj instanceof Map) {
			if (((Map) obj).isEmpty()) {
				result = true;
			}
		}
		if (obj instanceof List) {
			if (((List) obj).size() == 0 || ((List) obj).isEmpty()) {
				result = true;
			}
		}
		if (obj instanceof Set) {
			if (((Set) obj).isEmpty() || ((Set) obj).size() == 0) {
				result = true;
			}
		}
		return result;
	}

	/**
	 * ��ѯ���Ե�SQLע�����,�� ' ����Ϊ _
	 * 
	 * @param queryProperty
	 * @return
	 * @throws Exception
	 */
	public static String sqlInjectionFilter(String queryProperty)
			throws Exception {
		if (queryProperty != null && queryProperty.indexOf("'") != -1) {
			queryProperty = queryProperty.replaceAll("\'", "_");
		}
		return queryProperty;
	}

	/**
	 * �������Կ�����ʵ�ֶ�̬����
	 * 
	 * @param dbObj
	 * @param obj
	 * @throws Exception
	 * @author ZhangKW
	 * @date 2010-11-23
	 */
	public static void propertyDynamicUpdate(Object dbObj, Object obj)
			throws Exception {

		String[] fieldNameArray = PropertyNewUtil.getAllFieldName(obj);
		
		String[] fieldNameArray2=PropertyNewUtil.getAllFieldName(dbObj);
		
		for (int i = 0; i < fieldNameArray.length; i++) {
			String fieldName = fieldNameArray[i];
			
			for(int z=0;z<fieldNameArray2.length;z++) {
				if(fieldName.equals(fieldNameArray2[z])) {
					Object value = PropertyNewUtil.getFieldValueObj(obj, fieldName);
					if (value != null) {
						PropertyNewUtil.setFieldValueObj(dbObj, fieldName, value,value.getClass());
					}
				}
			}
		}
	}
	
	public static void setFieldValueObj(Object o, String field, Object value,Object type)
			throws Exception {
		Class<? extends Object> clazz = o.getClass();
		Method getMethod = clazz.getMethod(catchMethodName("get", field), null);
		getMethod.getClass();
		if(getMethod.getClass()==type) {
			Method setMethod = null; 
			if(value instanceof java.util.HashSet){
				setMethod = clazz.getMethod(catchMethodName("set", field),
						new Class[] {value instanceof java.util.HashSet ? Set.class : value.getClass()});
			}else{
				
				setMethod = clazz.getMethod(catchMethodName("set", field),
						new Class[] {value instanceof java.sql.Timestamp ? Date.class : value.getClass()});
			}
			setMethod.invoke(o, new Object[] { value });
		}
	}

	/**
	 * ��ȡ��������ָ�������������ֵ
	 * 
	 * @param o
	 *            ����Ķ���
	 * @param field
	 *            �����������
	 * @return
	 * @throws Exception
	 * @author ZhangKW
	 * @date 2010-11-23
	 */
	public static Object getFieldValueObj(Object o, String field)
			throws Exception {
		Method getMethod;
		Object returnObj;
		Class clazz = o.getClass();
		getMethod = clazz.getMethod(catchMethodName("get", field), null);
		returnObj = getMethod.invoke(o, null);
		return returnObj;
	}

	/**
	 * ���ö����ָ������ֵ
	 * 
	 * @param o
	 *            ����Ķ���
	 * @param field
	 *            �����������
	 * @param value
	 *            ���ֵ
	 * @throws Exception
	 * @author ZhangKW
	 * @date 2010-11-23
	 */
	public static void setFieldValueObj(Object o, String field, Object value)
			throws Exception {
		Class<? extends Object> clazz = o.getClass();
		Method setMethod = null; 
		if(value instanceof java.util.HashSet){
			setMethod = clazz.getMethod(catchMethodName("set", field),
					new Class[] {value instanceof java.util.HashSet ? Set.class : value.getClass()});
		}else{
			
			setMethod = clazz.getMethod(catchMethodName("set", field),
					new Class[] {value instanceof java.sql.Timestamp ? Date.class : value.getClass()});
		}
		setMethod.invoke(o, new Object[] { value });
	}

	/**
	 * ��ö����������ַ�����
	 * 
	 * @param obj
	 * @return
	 * @author ZhangKW
	 * @date 2010-11-23
	 */
	private static String[] getAllFieldName(Object obj) {
		ArrayList<String> methodList = new ArrayList<String>();
		Method[] methods = obj.getClass().getMethods();
		for (int i = 0; i < methods.length; i++) {
			Method method = methods[i];
			if (method.getName().startsWith("get")
					&& method.getReturnType() != null
					&& !"getClass".equals(method.getName()))
				methodList.add(catchFieldByMethodName(method.getName()));
		}
		return (String[]) methodList.toArray(new String[methodList.size()]);
	}

	/**
	 * ��ȡ�������
	 * 
	 * @param methodName
	 * @return
	 * @author ZhangKW
	 * @date 2010-11-23
	 */
	private static String catchFieldByMethodName(String methodName) {
		char[] charArray = methodName.substring(3).toCharArray();
		charArray[0] = Character.toLowerCase(charArray[0]);
		return new String(charArray);
	}

	/**
	 * ��ö��󷽷���
	 * 
	 * @param getterOrSetter
	 * @param fieldName
	 * @return
	 * @author ZhangKW
	 * @date 2010-11-22
	 */
	private static String catchMethodName(String getterOrSetter,
			String fieldName) {
		char[] charArray = fieldName.toCharArray();
		charArray[0] = Character.toUpperCase(charArray[0]);
		return getterOrSetter + new String(charArray);
	}

}
